//
//  QDAddCaseController.m
//  simulateQuickDoctor
//
//  Created by locklight on 17/3/26.
//  Copyright © 2017年 PipiXia. All rights reserved.
//

#import "QDAddCaseController.h"
#import "QDSicknessTypeView.h"
#import "QDSicknessDescriptionView.h"
#import "QDSicknessCasePictureView.h"
#import "QDSicknessTypeController.h"
#import "QDSicknessDetailController.h"
#import "QDDescListController.h"
#import "QDBannerTools.h"

@interface QDAddCaseController ()

/**
 *  疾病类型
 */
@property (nonatomic, strong) QDSicknessTypeView *illnessTypeView;

/**
 *  疾病细分
 */
@property (nonatomic, strong) QDSicknessTypeView *illnessChoiceView;

/**
 *  病例描述View
 */
@property (nonatomic, strong) QDSicknessDescriptionView *descView;

/**
 *  病例图片View
 */
@property (nonatomic, strong) QDSicknessCasePictureView *casePictureView;

/**
 *  记录哪种疾病
 */
@property (nonatomic, assign) NSInteger index;


@end

@implementation QDAddCaseController

@synthesize caseModel = _caseModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavigationBar];
    
    [self setupUI];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

/**
 *  设置navigationBar
 */
- (void)setNavigationBar {
    //定制返回按钮
    UIButton *button = [[UIButton alloc] init];
    button.frame = CGRectMake(0, 0, 50, 40);
    button.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [button setImage:[UIImage imageNamed:@"home_nav_button_back"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(didBackButton:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    //设置导航栏rightBarButtonItem
    UIBarButtonItem *completion =[[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(didClickSureButton:)];
    completion.tag = 1;
    UIBarButtonItem *delete=[[UIBarButtonItem alloc]initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(clickDeleteItem:)];
    self.navigationItem.rightBarButtonItems=@[completion,delete];
    delete.tag = 2;

}

/**
 *  初始化UI
 */
- (void)setupUI {
    [self.view addSubview:self.illnessTypeView];
    [self.view addSubview:self.illnessChoiceView];
    [self.view addSubview:self.descView];
    [self.view addSubview:self.casePictureView];
    
    //自动布局子控件
    [self.illnessTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_topLayoutGuide);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(40);
    }];
    
    [self.illnessChoiceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.illnessTypeView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(40);
    }];
    
    [self.descView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.illnessChoiceView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(200);
    }];
    
    [self.casePictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.descView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(150);
    }];
}

#warning mark   - 待完善

/**
 *  设置返回按钮事件
 */
- (void)didBackButton:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 *  设置确定按钮事件
 */
- (void)didClickSureButton:(UIBarButtonItem *)saveItem{
    //判断是否选择疾病类型
    if ([self.illnessTypeView.illnessChoiceString isEqualToString:@"请选择"]) {
        [QDBannerTools pp_BannerErrorTitle:@"确定选择病例前,请先选择疾病类型" andTime:1];
      
        return;
    }
    
    //判断是否选择疾病细分
    if ([self.illnessChoiceView.illnessChoiceString isEqualToString:@"请选择"]) {
        [QDBannerTools pp_BannerErrorTitle:@"确定选择病例前,请先选择疾病类型" andTime:1];
        return;
    }
    
    NSDateFormatter *formater = [[NSDateFormatter alloc]init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *time = [formater stringFromDate:[NSDate date]];
    
    //设置病例的创建时间
    self.caseModel.time = time;
    
    QDSickCaseModel *model = _caseModel;
    //回传block
    if (self.callbackModel) {
        NSLog(@"QDADMODEL ======%@",model);

        self.callbackModel(model,saveItem);
    }
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)clickDeleteItem:(UIBarButtonItem *)deleteItem{
    if(_caseModel == nil){
        [QDBannerTools pp_BannerErrorTitle:@"当前无病历数据" andTime:1];
        return;
    }
    QDSickCaseModel *model = _caseModel;
    if(self.callbackModel){
        self.callbackModel(model,deleteItem);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 懒加载子控件

- (QDSicknessTypeView *)illnessTypeView {
    if (!_illnessTypeView) {
        _illnessTypeView = [[QDSicknessTypeView alloc] init];
        _illnessTypeView.illnessTypeString = @"疾病类型";
        
        //添加点击手势
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickTypeView:)];
        [_illnessTypeView addGestureRecognizer:tapGesture];
    }
    return _illnessTypeView;
}

- (QDSicknessTypeView *)illnessChoiceView {
    if (!_illnessChoiceView) {
        _illnessChoiceView = [[QDSicknessTypeView alloc] init];
        _illnessChoiceView.illnessTypeString = @"疾病细分";
        
        // 添加点击手势
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickChoiceView:)];
        [_illnessChoiceView addGestureRecognizer:tapGesture];
    }
    return _illnessChoiceView;
}

- (QDSicknessDescriptionView *)descView {
    if (!_descView) {
        __weak typeof(self) weakSelf = self;
        
        _descView = [[QDSicknessDescriptionView alloc] init];
        //获取详细病情字符
        [_descView setCallBackTextString:^(NSString *text) {
            weakSelf.caseModel.illnessDescription = text;
        }];
        
        __weak typeof(_descView) weakDescView = _descView;
        //回调获取标签数组
        [_descView setDidClickButton:^(NSMutableArray *tempStringArray) {
            QDDescListController *descList = [[QDDescListController alloc] init];
            // 设置上一次选中的标签数组
            descList.cacheStringArray = tempStringArray;
            //回调选中的标签数组
            [descList setGetStringArr:^(NSMutableArray *stringArray) {
                //给dexView设置选中的标签
                weakDescView.stringArray = stringArray;
                //将选中的标签数组写入到病例模型里面
                weakSelf.caseModel.desArray = stringArray;
            }];
            [weakSelf.navigationController pushViewController:descList animated:YES];
        }];
    }
    return _descView;
}

- (QDSicknessCasePictureView *)casePictureView {
    if (!_casePictureView) {
        //创建添加图片的View
        _casePictureView = [[QDSicknessCasePictureView alloc] init];
        
        __weak typeof(self) weakSelf = self;
        //返回图片的数组
        [_casePictureView setCallBackImageArray:^(NSMutableArray *imageArr) {
            weakSelf.caseModel.picArray = imageArr;
        }];
        //返回HMimagePicker框架需要保存的信息
        [_casePictureView setCallBackPictureDict:^(NSDictionary *dict) {
            weakSelf.caseModel.pictureDict = dict;
        }];
    }
    return _casePictureView;
}

- (QDSickCaseModel *)caseModel {
    if (!_caseModel) {
        _caseModel = [[QDSickCaseModel alloc] init];
    }
    return _caseModel;
}

/**
 *  点击疾病类型view响应事件
 */
- (void)didClickTypeView:(UIView *)view {
    QDSicknessTypeController *illnessTypeController = [[QDSicknessTypeController alloc] init];
    
    [illnessTypeController setGetTypeString:^(NSString *typeString,NSInteger index) {
        //获取疾病类型
        self.illnessTypeView.illnessChoiceString = typeString;
        self.illnessChoiceView.illnessChoiceString = @"请选择";
        self.index = index;
        self.caseModel.caseCatogory = typeString;
    }];
    [self.navigationController pushViewController:illnessTypeController animated:YES];
}

/**
 *  点击疾病细分view响应事件
 */
- (void)didClickChoiceView:(UIView *)view {
    if (![self.illnessTypeView.illnessChoiceString isEqualToString:@"请选择"]) {
        QDSicknessDetailController *detailController = [[QDSicknessDetailController alloc] init];
        [detailController setGetIllnessString:^(NSString *String
                                                , NSInteger index) {
            self.illnessChoiceView.illnessChoiceString = String;
            self.caseModel.caseDetail = String;
            self.caseModel.catogoryIndex = index;
        }];
        
        detailController.illnessIndex = self.index;
        [self.navigationController pushViewController:detailController animated:YES];
    }else{
        [QDBannerTools pp_BannerErrorTitle:@"请选择疾病类型" andTime:1];
    }
}

/**
 *  从病例模型里面读取数据
 */
- (void)setCaseModel:(QDSickCaseModel *)caseModel {
    _caseModel = caseModel;
    
    self.illnessTypeView.illnessChoiceString = caseModel.caseCatogory;
    self.illnessChoiceView.illnessChoiceString = caseModel.caseDetail;
    self.casePictureView.pictureDict = caseModel.pictureDict;
    self.descView.stringArray = caseModel.desArray;
    self.descView.desText = caseModel.illnessDescription;
    self.index = caseModel.catogoryIndex;
}


@end
